#include "general.h"

#include <buolabase_basicio.h>

CFileData::CFileData()
    : mNode(nullptr)
{
}

CFileData::CFileData(const CXMLNode &pNode)
    : mNode(new CXMLNode(pNode))
{
}

CFileData::~CFileData()
{
}

void CFileData::Read(COldBuolaStream *pStream)
{
    //first the number of keys
    int lCount;
    pStream->ReadNum(&lCount,4);
    //then all the keys
    for(int i=0;i<lCount;i++)
    {
        std::string lName,lValueStr;
        //first the name
        pStream->ReadStr(lName);
        //then the value
        pStream->ReadStr(lValueStr);

        mData[lName]=UValue(utf32(lValueStr));
    }
}

void CFileData::Write(COldBuolaStream *pStream)
{
    //first the number of keys
    pStream->WriteNum(mData.size(),4);
    //then all the keys
    for(std::unordered_map<std::string,UValue>::iterator i=mData.begin();
                                                i!=mData.end();++i)
    {
        //first the name
        pStream->WriteStr(i->first);
        //then the value
        pStream->WriteStr(i->second.ToStringWithType());
    }
}

void CFileData::GetValue(const std::string &pKey,UValue &pValue)
{
    try
    {
        pValue=mData[pKey];
    }
    catch(XNotFound&)
    {
    }
}

void CFileData::SetValue(const std::string &pKey,const UValue &pValue)
{
    mData[pKey]=pValue;
}
